package com.android.bookgarden.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.bookgarden.R;
import com.android.bookgarden.bean.BtItem;
import com.android.bookgarden.bean.SearchBean;
import com.android.bookgarden.recycleview.BaseViewHolder;

import java.util.List;

/**
 * Created by zouqh on 2018/3/30.
 */

public class AvocationAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private static final int TYPE_FOOTER = 0;
    private static final int ITEM_IMAGE = 1;
    private static final int ITEM_IMAGES = 2;

    private List<BtItem> datas;
    private Context mContext;
    private int viewFooter;
    private View footerView;
    private OnItemClickListener mOnItemClickListener;
    private int itemWidth;
    private boolean isDelete;

    public AvocationAdapter(List<BtItem> datas, Context context) {
        this.datas = datas;
        this.mContext = context;
    }

    public void replaceAll(List<BtItem> elements) {
        if (datas.size() > 0) {
            datas.clear();
        }
        datas.addAll(elements);
        notifyDataSetChanged();
    }

    public void addAll(List<BtItem> elements) {
        datas.addAll(elements);
        notifyDataSetChanged();
    }
    public void setDelete(boolean city){
        this.isDelete = city;
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_IMAGE) {
            return new BaseViewHolder(LayoutInflater.from(mContext).inflate(R.layout.interest_item, parent, false));
        } else {
            footerView = LayoutInflater.from(mContext).inflate(viewFooter, parent, false);
            return new BaseViewHolder(footerView);
        }
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        if (!(viewFooter != 0 && position == getItemCount() - 1)) {
            if (mOnItemClickListener != null) {
                holder.setOnClickListener(R.id.delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(position, holder);
                    }
                });
            }
            int type = getItemViewType(position);
            BtItem item = datas.get(position);
            try {
                if (type == ITEM_IMAGE) {
                    holder.setText(R.id.name, item.getName());
                   if (item.getName().contains("推荐")){
                       holder.setVisible(R.id.delete,false);
                   }else{
                       holder.setVisible(R.id.delete,isDelete);
                   }
                } else {

                }
            } catch (Exception e) {
            }
        } else {
            if (mOnItemClickListener != null) {
                holder.setOnClickListener(R.id.delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(datas.size() + 1, holder);
                    }
                });
//                holder.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        mOnItemClickListener.onItemClick(datas.size() + 1, holder);
//                    }
//                });
            }
        }

    }

    @Override
    public int getItemCount() {
        int count = (datas == null ? 0 : datas.size());
        if (viewFooter != 0) {
            count++;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        int type = ITEM_IMAGE;
        if (viewFooter != 0 && position == getItemCount() - 1) {
            type = TYPE_FOOTER;
            return type;
        }
        return type;
    }

    public void addFooterView(int footerView) {
        this.viewFooter = footerView;
        notifyItemInserted(getItemCount() - 1);
    }

    public void setFooterVisible(int visible) {
        if (null != footerView) {
            footerView.setVisibility(visible);
        }

    }

    //设置点击事件
    public void setOnItemClickLitener(OnItemClickListener mLitener) {
        mOnItemClickListener = mLitener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, BaseViewHolder viewHolder);
    }
}